package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.SubcategoryDTO;
import com.lastproject.ejb.entities.Subcategory;

/**
 * Session Bean implementation class SubcategoryConversion
 */
@Stateless
@LocalBean
public class SubcategoryConversion {
	@PersistenceContext
	EntityManager em;

	public SubcategoryDTO fromEntity(Subcategory u) {

		SubcategoryDTO subcategory = new SubcategoryDTO();
		subcategory.setId(u.getId());
		subcategory.setName(u.getName());

		return subcategory;
	}

	public Subcategory fromDTO(SubcategoryDTO u) {

		Subcategory subcategory = em.find(Subcategory.class, u.getId());

		if (subcategory == null) {
			subcategory = new Subcategory();
		}

		subcategory.setId(u.getId());
		subcategory.setName(u.getName());
		
		return subcategory;
	}

}
